package com.market.service.admin.impl;


import com.github.pagehelper.PageInfo;
import com.market.bean.common.CommonData;
import com.market.bean.vo.admin.AdminRegionListCitieVo;
import com.market.bean.vo.admin.AdminRegionListProvinceVo;
import com.market.mapper.MarketRegionMapper;
import com.market.service.admin.AdminRegionListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;

/**
 * @Author:WalterChan
 * @Decription:AdminRegionListServiceImpl
 * @Date Created in 2022-12-29-16:30
 */
@Service
public class AdminRegionListServiceImpl implements AdminRegionListService {
    @Autowired
    MarketRegionMapper marketRegionMapper;

    @Override
    public CommonData list() {

        //先整合城市和区的表
        LinkedList<AdminRegionListCitieVo> cityList= marketRegionMapper.selectCityWithDistrict();
//        再整合省份和城市的表
        LinkedList<AdminRegionListProvinceVo> provinceList =marketRegionMapper.selectProvinWithCity();
//        合并2个表

//        遍历省份表
        for (AdminRegionListProvinceVo provinceVo : provinceList) {
//            遍历省份内的市
            LinkedList<AdminRegionListCitieVo> children = provinceVo.getChildren();
            for (AdminRegionListCitieVo child : children) {
//                遍历城市表，如果城市的id==child 那么就 省表内的城市的child  = 市表的child
                for (AdminRegionListCitieVo adminRegionListCitieVo : cityList) {
                    if (child.getId()==adminRegionListCitieVo.getId()){
                        child.children=adminRegionListCitieVo.children;

                    }
                }

            }
        }
        PageInfo<AdminRegionListProvinceVo> pageInfo = new PageInfo<>(provinceList);
        return CommonData.data(pageInfo);

    }

}
